package com.orientechnologies.orient.distributed.context.coordination.message;

import com.orientechnologies.orient.distributed.context.OSyncId;
import com.orientechnologies.orient.distributed.db.OrientDBDistributed;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class OSyncData implements OStructuralMessage {

  public final byte[] data;
  private final OSyncId syncId;
  private final boolean finished;

  /**
   * The data need to be immutable, copy it if can mutate before passing
   *
   * @param data
   */
  public OSyncData(OSyncId syncId, byte[] data, boolean finished) {
    this.syncId = syncId;
    this.data = data;
    this.finished = finished;
  }

  @Override
  public void execute(OrientDBDistributed ctx) {
    ctx.receiveSyncData(this.syncId, this.data, this.finished);
  }

  @Override
  public void serialize(DataOutput out) throws IOException {
    this.syncId.writeNetwork(out);
    out.writeInt(this.data.length);
    out.write(this.data);
    out.writeBoolean(this.finished);
  }

  @Override
  public short getType() {
    return 10;
  }

  public static OSyncData fromNetwork(DataInput input) throws IOException {
    OSyncId syncId = OSyncId.readNetwork(input);
    int size = input.readInt();
    byte[] data = new byte[size];
    input.readFully(data);
    boolean finished = input.readBoolean();
    return new OSyncData(syncId, data, finished);
  }

  public byte[] getData() {
    return data;
  }

  public OSyncId getSyncId() {
    return syncId;
  }

  public boolean isFinished() {
    return finished;
  }
}
